<?php
session_start();
isset($PHPSESSID)?session_id($PHPSESSID):$PHPSESSID = session_id();
setcookie('PHPSESSID',$PHPSESSID,time()+3156000);
$_SESSION['id'] = $PHPSESSID;
include 'login.php';
?>
<html>
<title>随机聊天</title>
<head>
<meta charset="utf-8"/>
<script type="text/javascript" src="prototype.js">
</script>
<script type="text/javascript">
new PeriodicalExecuter(istalk,0.3);
new PeriodicalExecuter(update_activity,180);
var pe;
var flag = 1;
function istalk(){
    new Ajax.Request("istalk.php",{
	onSuccess:function(request){
	    if(request.responseText == "TRUE")
	    {
		update();
	    }
	    else if(request.responseText == "TALK")
	    {
		if(flag == 1)
		{
		    document.getElementById("content").value += "找到用户，建立连接成功\n";// 显示进入聊天状态
		    flag = 0;
		}
	    }
	}
    });
}
function update_activity(){
    new Ajax.Request("lastactivity.php");
}
function update(){
    new Ajax.Request("update.php",{
	onSuccess:function(request){
	    document.getElementById("content").value+="you:"+request.responseText+"\n";
	}
    });
}
function find_user(){
    flag = 1;
    document.getElementById("content").value = "正在寻找用户，请稍等...\n";
    new Ajax.Request("finduser.php",{
	onSuccess:function(request){
	    if(request.responseText == "TRUE")
		;
	}
    });
}
function send(){
    var url = "send.php";
    var msg = document.getElementById("msg").value;
    document.getElementById("content").value+="me:"+msg+"\n";
    document.getElementById("msg").value="";
    new Ajax.Request(url,{
	method:'post',
	parameters:{message:msg},
	onSuccess:function(request){
	    if(request.responseText == "TRUE"){
	    }
	}
    });
}
function close_window() { new Ajax.Request("logout.php"); } </script> </head>
<body onunload="close_window()">
<textarea id="content" rows="20" cols="80">
</textarea>
<form>
<textarea id="msg" rows="3" cols="80">
</textarea>
</br>
<button type="button" onclick="send()">Send</button>
<button type="button" onclick="find_user()">FindUser</button>
</form>

</body>
</html>